package com.i8office.android.zhihuixinwu.module.presenter;

import android.text.TextUtils;

import com.cyan.tools.callback.SimpleCallback;
import com.cyan.tools.util.RxBus;
import com.i8office.android.zhihuixinwu.bean.Code;
import com.i8office.android.zhihuixinwu.bean.Empl;
import com.i8office.android.zhihuixinwu.bean.Favorite;
import com.i8office.android.zhihuixinwu.bean.WorkSheet;
import com.i8office.android.zhihuixinwu.bean.WorkSheetNew;
import com.i8office.android.zhihuixinwu.module.contract.AddOpinionContract;
import com.i8office.android.zhihuixinwu.module.contract.DetailContract;
import com.i8office.android.zhihuixinwu.module.contract.StaffContract;
import com.i8office.android.zhihuixinwu.module.model.AddOpinionModel;
import com.i8office.android.zhihuixinwu.module.model.DetailModel;
import com.i8office.android.zhihuixinwu.module.model.StaffModel;
import com.socks.library.KLog;
import com.zejian.emotionkeyboard.utils.LogUtils;

import java.util.Map;

/**
 * Created by pc on 2018/7/7.
 */

public class DetailPresenter extends BasePresenterImpl<DetailContract.View> implements DetailContract.Presenter {
    private DetailContract.Model model;
    private AddOpinionContract.Model saveCellModel;
    private StaffContract.Model sendModel;

    public DetailPresenter(DetailContract.View view) {
        super(view);
        model = new DetailModel();
        saveCellModel = new AddOpinionModel();
        sendModel = new StaffModel();
    }

    @Override
    public void getDetail(String mid) {
        addDisposable(model.getDetail(new SimpleCallback<WorkSheet>(mView) {
            @Override
            public void requestSuccess(WorkSheet workSheet) {
                mView.showDetail(workSheet);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void getDetailDep(String mid) {
        addDisposable(model.getDetailDep(new SimpleCallback<WorkSheetNew>(mView) {
            @Override
            public void requestSuccess(WorkSheetNew workSheet) {
                mView.showDetailDep(workSheet);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void addFavorite(String mid) {
        addDisposable(model.addFavorite(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code code) {
                if (code == null) {
                    return;
                }
                if (code.isSucceed()) {
                    mView.collectResult(true);
                } else {
                    if (!TextUtils.isEmpty(code.getMessage())) {
                        mView.toast(code.getMessage());
                    }
                }
            }

            @Override
            public void beforeRequest() {
            }

            @Override
            public void requestComplete() {

            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void cancelFavorite(String mid) {
        addDisposable(model.cancelFavorite(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code code) {
                if (code == null) {
                    return;
                }
                if (code.isSucceed()) {
                    mView.collectResult(false);
                } else {
                    if (!TextUtils.isEmpty(code.getMessage())) {
                        mView.toast(code.getMessage());
                    }
                }
            }

            @Override
            public void beforeRequest() {
            }

            @Override
            public void requestComplete() {

            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void addDepFavorite(String mid) {
        addDisposable(model.addDepFavorite(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code code) {
                if (code == null) {
                    return;
                }
                if (code.isSucceed()) {
                    mView.collectResult(true);
                } else {
                    if (!TextUtils.isEmpty(code.getMessage())) {
                        mView.toast(code.getMessage());
                    }
                }
            }

            @Override
            public void beforeRequest() {
            }

            @Override
            public void requestComplete() {

            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void cancelDepFavorite(String mid) {
        addDisposable(model.cancelDepFavorite(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code code) {
                if (code == null) {
                    return;
                }
                if (code.isSucceed()) {
                    mView.collectResult(false);
                } else {
                    if (!TextUtils.isEmpty(code.getMessage())) {
                        mView.toast(code.getMessage());
                    }
                }
            }

            @Override
            public void beforeRequest() {
            }

            @Override
            public void requestComplete() {

            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void saveCell(Map<String, String> params) {
        addDisposable(saveCellModel.saveCell(new SimpleCallback<Code>(mView) {
            @Override
            public void requestSuccess(Code data) {
                if (data == null) {
                    return;
                }
                if (data.isSucceed()) {
                    mView.saveCellSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.onfailed();
            }
        }, params));
    }

    @Override
    public void messageHandler(Map<String, String> params) {
        addDisposable(model.messageHandler(new SimpleCallback<Empl>(mView) {
            @Override
            public void requestSuccess(Empl empl) {
                mView.messageHandlerResult(empl);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.onfailed();
            }
        }, params));
    }

    @Override
    public void send(Map<String, String> params) {
        addDisposable(sendModel.send(new SimpleCallback<Code>(mView) {
            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
                mView.onfailed();
            }

            @Override
            public void requestSuccess(Code data) {
                super.requestSuccess(data);
                if (data.isSucceed()) {
                    mView.sendSuccess();
                } else {
                    if (!TextUtils.isEmpty(data.getMessage())) {
                        mView.toast(data.getMessage());
                    }
                }

            }
        }, params));
    }

    @Override
    public void open(Map<String, String> params) {
        sendModel.open(new SimpleCallback<String>(mView) {
            @Override
            public void beforeRequest() {

            }

            @Override
            public void requestError(String msg) {
//                mView.onfailed();
            }

            @Override
            public void requestSuccess(String data) {
            }
        }, params);
    }

    @Override
    public void sendNotification(Map<String, String> map) {
        sendModel.sendNotification(new SimpleCallback<String>(mView) {
            @Override
            public void beforeRequest() {

            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                KLog.e("发送消息失败" + msg);
            }

            @Override
            public void requestSuccess(String data) {
                super.requestSuccess(data);
                KLog.e("发送消息成功" + data);
            }
        }, map);
    }

    @Override
    public void isFavorite(String mid) {
        addDisposable(model.isFavorite(new SimpleCallback<Favorite>(mView) {
            @Override
            public void requestSuccess(Favorite favorite) {
                mView.showFavorite(favorite);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

    @Override
    public void isDepFavorite(String mid) {
        addDisposable(model.isDepFavorite(new SimpleCallback<Favorite>(mView) {
            @Override
            public void requestSuccess(Favorite favorite) {
                mView.showFavorite(favorite);
            }

            @Override
            public void requestError(String msg) {
                super.requestError(msg);
                mView.toast(msg);
            }
        }, mid));
    }

}
